// 用于列表的渲染
<template>
  <div>
    <el-table :data="goodsList" border style="width: 100%">
      <el-table-column prop="id" label="商品编号" width="180">
      </el-table-column>
      <el-table-column prop="goodsname" label="商品名称" width="180">
      </el-table-column>
      <el-table-column prop="price" label="商品价格"> </el-table-column>
      <el-table-column prop="market_price" label="市场价格" width="180">
      </el-table-column>
      <el-table-column label="图片" width="180">
        <template slot-scope="item">
          <div>
            <img
              class="img"
              :src="
                item.row.img
                  ? $imgUrl + item.row.img
                  : 'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1870521716,857441283&fm=26&gp=0.jpg'
              "
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="是否新品">
        <template slot-scope="item">
          <!-- tag标签 -->
          <div>
            <el-tag v-if="item.row.isnew == 1" type="success">是</el-tag>
            <el-tag v-else type="danger">否</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="是否热卖">
        <template slot-scope="item">
          <!-- tag标签 -->
          <div>
            <el-tag v-if="item.row.ishot == 1" type="success">是</el-tag>
            <el-tag v-else type="danger">否</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="item">
          <!-- tag标签 -->
          <div>
            <el-tag v-if="item.row.status == 1" type="success">启 用</el-tag>
            <el-tag v-else type="danger">禁 用</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="item">
          <div>
            <el-button type="primary" size="small" round @click="edit(item.row.id)"
              >编辑</el-button
            >
            <!-- {{item.row}}    代表要删除的每一条数据 -->
            <!-- 可以根据指定的 菜单编号(id) 进行删除-->
            <el-button
              type="danger"
              size="small"
              round
              @click="del(item.row.id)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :page-size="goodsSize"
      background
      layout="prev, pager, next"
      :total="goodsCount"
      @current-change="currentChange"
    >
    </el-pagination>
  </div>
</template>

<script>
import { mapActions, mapGetters } from "vuex";
// 引入接口文档
import { getGoodsDelete } from "../../util/axios";
export default {
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      goodsList: "goods/getGoodsList",
      goodsSize: "goods/getGoodsSize",
      goodsCount: "goods/getGoodsCount"
    })
  },
  mounted() {
    //组件一加载触发行动
    //调取商品列表
    this.getGoodsAction();
    //调取总条数
    this.getCountAction();
  },
  methods: {
    ...mapActions({
      getGoodsAction: "goods/getGoodsAction",
      getCountAction: "goods/getCountAction",
      changePage: "goods/changePageAction"
    }),
    //封装编辑事件，传递id给role这个父组件
    edit(id) {
      this.$emit("edit", id);
    },
    //封装一个删除事件
    del(id) {
      this.$confirm("你确定要删除这条数据吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          //调取删除接口
          getGoodsDelete({
            id
          }).then(res => {
            if (res.data.code === 200) {
              this.$message.success(res.data.msg);
              //删除之后要重新调取列表
              this.getGoodsAction();
              //重新调取总条数
              this.getCountAction();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    currentChange(e) {
      this.changePage(e);
    }
  }
};
</script>

<style lang="" scoped>
.img {
  width: 120px;
  height: 120px;
}
</style>
